package engine.newUi
{
	import engine.data.ModelLocator;
	import engine.flag.PlayerActionFlag;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.scene.SceneClickLogic;
	import engine.scene.SceneManager;
	import engine.scene.player.BasePlayerModel;
	import engine.scene.player.NpcModel;
	
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	/**
	 * 雪人逻辑 
	 * @author gf
	 * 
	 */	
	
	public class MakeSnowLogic
	{
		private static var _self:MakeSnowLogic
		
		private var _myDis:MakeSnowUI;
		private var _model:ModelLocator;
		
		private var _timer:Timer;
		private var _width:int;
		
		private var time:int;
		
		private var barWidth	:Number		=	0;
		private var barPoint	:Number		=	0;

		private var clickNPC	:NpcModel;
		
		private var _isShow	:Boolean	=	false;
		
		public function MakeSnowLogic()
		{
			_model				=	ModelLocator.getInstance();
			_myDis				=	MakeSnowUI.getInstance();
		}

		public function get isShow():Boolean
		{
			return _isShow;
		}

		public static function getInstance():MakeSnowLogic
		{
			if(_self == null)
			{
				_self = new MakeSnowLogic();
			}
			return _self;
		}
		
		public function init():void
		{
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_MAKE_SNOWBALL),onShow);
			barWidth	=	_myDis.loading["loadingLine"].width;
			barPoint	=	_myDis.loading["loadingPoint"].x;
		}
		
		public function addListener():void 
		{
		}
		
		public function removeListener():void
		{
			reset();
			_timer.removeEventListener(TimerEvent.TIMER_COMPLETE, timerCompleteHandler);
		}
		
		private function onShow(e:CustomMsgEvent):void
		{
//			_myDis.show();
			_isShow	=	true;
			clickNPC	=	SceneClickLogic.currentNPC as NpcModel;
			
			time	=	e.pList[0];
			_timer = new Timer(50,20*time);
			_timer.addEventListener(TimerEvent.TIMER, timerHandler);
			_timer.addEventListener(TimerEvent.TIMER_COMPLETE, timerCompleteHandler);
			_timer.start();
			
			clickNPC.effectSpace.addChild(_myDis.loading);
			if(clickNPC.status==PlayerActionFlag.NormalWait) clickNPC.status=PlayerActionFlag.Run;
			_myDis.loading.x = clickNPC.playerMovie.overhead.x  - _myDis.loading.width * .5;
			_myDis.loading.y = clickNPC.namePlace.y + 5;
		}
		
		private function timerHandler(t:TimerEvent):void
		{
			_width	= _myDis.loading["loadingLine"].width;
			if(_width <= 0)
			{
				timerCompleteHandler();
				return;
			}
			_width	-=	barWidth/(time*20);
			_myDis.loading["loadingLine"].width	=	_width;
			_myDis.loading["loadingPoint"].x		=	_myDis.loading["loadingLine"].x - _width + 20;
		}
		
		private function timerCompleteHandler(t:TimerEvent=null):void
		{
			reset();
			clickNPC.effectSpace.removeChild(_myDis.loading);
//			_myDis.hideCur();
		}
		
		public function stopMake():void
		{
			timerCompleteHandler();
			clickNPC.status=PlayerActionFlag.NormalWait;
		}
		
		private function reset():void
		{
			_isShow	=	false;
			_timer.stop();
			_timer=null;
			_myDis.loading["loadingLine"].width	=	barWidth;
			_myDis.loading["loadingPoint"].x		=	barPoint;
		}
	}
}